export class MigrationUtil {
  constructor(public queryInterface: any, public Sequelize: any) {}

  async addColumn(table: string, field: string, option: any): Promise<void> {
    const tables = await this.queryInterface.showAllTables();
    if (tables.indexOf(table) < 0) {
      return;
    }
    const attributes = await this.queryInterface.describeTable(table);
    if (attributes[field]) {
      return;
    }
    await this.queryInterface.addColumn(table, field, option);
    console.log(`${table}表增加${field}字段`);
  }

  async changeColumn(table: string, field: string, option: any): Promise<void> {
    const tables = await this.queryInterface.showAllTables();
    if (tables.indexOf(table) < 0) {
      return;
    }
    const attributes = await this.queryInterface.describeTable(table);
    if (!attributes[field]) {
      return;
    }
    await this.queryInterface.changeColumn(table, field, option);
    console.log(`${table}表修改${field}字段`);
  }

  async renameColumn(table: string, field: string, newField: string): Promise<void> {
    const tables = await this.queryInterface.showAllTables();
    if (tables.indexOf(table) < 0) {
      return;
    }
    const attributes = await this.queryInterface.describeTable(table);
    if (!attributes[field]) {
      return;
    }
    await this.queryInterface.renameColumn(table, field, newField);
    console.log(`${table}表字段${field}改为${newField}`);
  }
}
